package com.kayt.cyberBar.cuser.business.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.common.property.PublicCodeConfigInfo;
import com.kayt.cyberBar.cuser.business.dao.ITaskModelDao;
import com.kayt.cyberBar.cuser.business.entity.po.BTaskModel;

@Repository("taskModelDao")
public class TaskModelDaoImpl implements ITaskModelDao {
	@Autowired
	private EntityDao entityDao;
	@Autowired
	private PublicCodeConfigInfo configInfo;

	@SuppressWarnings("unused")
	@Override
	public Long save(BTaskModel entity) throws SQLException {
		if (entity != null) {
			return entityDao.save(entity);
		}else{
			return null;
		}
	}

	@Override
	public Integer update(BTaskModel entity) throws SQLException {
		if (entity != null) {
			entityDao.update(entity);
			return 0;
		} else {
			return -1;
		}
	}

	@Override
	public Integer delete(Long id) {
		try {
			if (id != null) {
				BTaskModel bTaskModel = findById(id);
				if (bTaskModel != null) {
					entityDao.delete(bTaskModel);
					return 0;
				} else {
					return -1;
				}
			} else {
				return -1;
			}
		} catch (Exception e) {
			return -1;
		}
	}

	@Override
	public BTaskModel findById(Long id) {
		if (id != null) {
			return entityDao.findById(id, BTaskModel.class);
		} else {
			return null;
		}
	}

	@Override
	public PageData<BTaskModel> findByStoreCode(int pageNum, int pageSize, String storeCode) throws SQLException {
		String hql = "o.storeCode = '"+storeCode+"' and o.statusCode != '"+configInfo.getPublicStatusDelete()+"'";
		return entityDao.getPagingData(BTaskModel.class, pageNum, pageSize, hql, null, null);
	}

	@Override
	public Boolean checkModelNameExist(String modelName, String storeCode) throws SQLException {
		String hql = "select * from b_task_model where TASK_MODEL_NAME ='"+modelName+"' and STORE_CODE = '"+storeCode+"'";
		List<BTaskModel> modelList = entityDao.getPagingDataBySql(BTaskModel.class, -1, -1, hql, null);
		if(modelList == null || modelList.size() == 0){
			return false;
		}else{
			return true;
		}
	}

	/* (non-Javadoc)
	 * @see com.kayt.cyberBar.cuser.business.dao.ITaskModelDao#findByCode(java.lang.String)
	 */
	@Override
	public BTaskModel findByCode(String code) throws SQLException {
		return entityDao.getObj(BTaskModel.class, " o.taskModelCode = ?", new Object[]{code});
	}

}
